﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CMTVision.Widgets.Modules
{
    //[System.Web.UI.ViewStateModeById]
    public partial class BaseWidgetContainer : CMTVision.Widgets.Impls.WidgetContainer
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
                SettingsOpen = false;
            }
            if (this.IsPostBack || this.Page.IsAsync)
            {
                if (SettingsOpen)
                {
                    Widget.Edit();
                }
            }

            editWidgetAnchor.Attributes["onclick"] = "return editAnchorClick('" + editWidgetAnchor.ClientID + "');";
        }

        #region Override

        protected override Control GetWidgetHolder()
        {
            return widgetHolder;
        }

        protected override Control GetDraggableHandle()
        {
            return widgetHeader;
        }

        #endregion

        #region Bind Data

        protected void BindData()
        {
            approveCircleWidgetAnchor.Visible = IsInEditMode;
            deleteWidgetAnchor.Visible = IsInEditMode;
            editWidgetAnchor.Visible = IsInEditMode;
            editWidgetAnchor.Attributes["class"] = Widget.IsInEditting ? "editwidget widget_closeedit" : "editwidget widget_openedit";
            saveWidgetAnchor.Visible = Widget.IsInEditting;
        }

        #endregion

        #region Edit Button Event Handle

        protected void editAnchor_Click(object sender, EventArgs e)
        {            
            if (Widget.IsInEditting)
            {
                editWidgetAnchor.Attributes["class"] = "editwidget widget_openedit";
                Widget.ForceRefresh();
                SettingsOpen = false;
            }
            else
            {
                editWidgetAnchor.Attributes["class"] = "editwidget widget_closeedit";
                Widget.Edit();
                SettingsOpen = true;

            } 
            saveWidgetAnchor.Visible = Widget.IsInEditting;
            widgetContainerUpdatePanel.Update();
        }

        protected void collapseAnchor_Click(object sender, EventArgs e)
        {
            bool inExpendStatus = collapseWidgetAnchor.Attributes["class"].IndexOf("widget_collapse") >= 0;
            collapseWidgetAnchor.Attributes["class"] = inExpendStatus ? "editwidget widget_expend" : "editwidget widget_collapse";
            widgetHolder.Visible = !inExpendStatus;
            widgetContainerUpdatePanel.Update();
        }

        protected void deleteAnchor_Click(object sender, EventArgs e)
        { 
            Widget.Delete();
            this.Visible = false;
            widgetContainerUpdatePanel.Update();
        }

        protected void saveAnchor_Click(object sender, EventArgs e)
        {
            Widget.Save();
            widgetContainerUpdatePanel.Update();
        }

        protected void lockAnchor_Click(object sender, EventArgs e)
        {
            string lockAnchorCss = " "+lockWidgetAnchor.Attributes["class"]+" ";
            string mainContainerCss =" "+ MainContainer.Attributes["class"]+" ";
            bool isLocked = lockAnchorCss.Contains(" widget_unlock ");

            lockWidgetAnchor.Attributes["class"] = isLocked ? "editwidget widget_lock" : "editwidget widget_unlock";
            MainContainer.Attributes["class"] = (isLocked ? mainContainerCss.Replace("locked", "") : mainContainerCss+"locked").Trim();

            var parent = MainContainer.Parent;
            while (!(parent == null || parent is UpdatePanel))
            {
                parent = parent.Parent;
            }
            if (parent is UpdatePanel)
            {
                (parent as UpdatePanel).Update();
            }
            //widgetContainerUpdatePanel.Update();  
        }
        #endregion

        #region Override Refresh

        public override void ForceRefresh()
        {
            base.ForceRefresh();
            BindData();
            widgetContainerUpdatePanel.Update();
        }

        #endregion
    }
}